//
//  AllScrollVViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/17.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "AllScrollVViewController.h"
#import "SGSegmentedControl.h"
#import "ChiledViewController.h"
@interface AllScrollVViewController ()<UIScrollViewDelegate, SGSegmentedControlDelegate>
@property (nonatomic, strong) SGSegmentedControl *SG;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic,strong)NSMutableArray *titleVCArr;//子控制器的名称数组

@end

@implementation AllScrollVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleVCArr = [NSMutableArray new];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initTitleWithTitle:_titleStr];
    // 1.添加所有子控制器
    [self getFindClassificationBeanList];
}

- (void)setupSegmentedControl {
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titleVCArr.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    //添加第一显示的控制器
    ChiledViewController *oneVC = [[ChiledViewController alloc] init];
    oneVC.oneKindsid = _parentId;
    oneVC.twoKindsid = @"";
    [self.mainScrollView addSubview:oneVC.view];
    [self addChildViewController:oneVC];
    
    //scrollV title控制器
    NSMutableArray *titles = [NSMutableArray new];
    for (NSDictionary *dic in _titleVCArr) {
        [titles addObject:dic[@"name"]];
    }
    self.SG = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(0,0, self.view.frame.size.width, 44) delegate:self segmentedControlType:(SGSegmentedControlTypeScroll) titleArr:titles];
    _SG.titleColorGradualChange = YES;
    [self.SG setIndicatorColor:mainColor];
    [self.SG setTitleColorStateSelected:mainColor];
    [self.SG setTitleColorStateNormal:[UIColor blackColor]];
    [self.view addSubview:_SG];
    
}

- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

// 循环添加所有子控制器
- (void)setupChildViewController {

    //循环 并将参数传递过去
    for (int i = 0; i < self.titleVCArr.count; i++) {
        ChiledViewController * vc = [[ChiledViewController alloc] init];
        vc.oneKindsid = _parentId;
        vc.twoKindsid = self.titleVCArr[i][@"id"];
        [self addChildViewController:vc];
    }
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    // 2.把对应的标题选中
    [self.SG titleBtnSelectedWithScrollView:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.SG titleBtnColorGradualChangeScrollViewDidScroll:scrollView];
}


-(void)getFindClassificationBeanList
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/find/findClassificationBeanList" Dic:@{@"type":@"1",@"parentId":_parentId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                NSLog(@"res:%@",responseDic);
                if ([responseDic[@"success"] boolValue]) {
                    [_titleVCArr addObjectsFromArray:responseDic[@"data"]];
                    [self setupChildViewController];
                    [self setupSegmentedControl];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end

